package com.ymatou.iapi.liveinfo.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveinfo.parameter.GetProductListByLiveIdBean;
import com.ymatou.iapi.liveinfo.parameter.SearchTypeEnum;
import com.ymatou.iapi.liveinfo.service.GetProductListByLiveIdCall;
import com.ymatou.iapi.sellerproduct.parameter.BrandBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.utils.EnvSetup;
/**
*根据直播Id获取商品Id列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetProductListByLiveId{
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	private static GetProductListByLiveIdBean bean;
	private static GetProductListByLiveIdCall call;
	private LiveProductsWapper liveProductsWapper=new LiveProductsWapper();
	private SearchTypeEnum searchTypeEnum;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据直播Id获取商品Id列表");
	}

	@Before
	public void caseUp() {
		bean=new GetProductListByLiveIdBean();
		call=new GetProductListByLiveIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductListByLiveId_001() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:全部,返回全部有效的直播商品id列表");
		try {
		    int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176700);
		   bean.setSearchType(searchTypeEnum.All);
			call.setData(bean);
			call.callService();	
			GetVerify(bean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_002() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌名称,并且查询关键字为空,返回全部有效的直播商品id列表");
		try {
		    int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
		    BrandBean band=SellerProductCallService.getBrandInfo(13217);
		    liveProductsWapper.updatePBrandByLiveAndBrank(lid,band.getBrandId(),band.getBrandName());
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			bean.setSearchType(searchTypeEnum.BrandName);
			bean.setKeyword("");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_003() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌名称,并且查询关键字不为空,返回查询关键字的有效的直播商品id列表");
		try {
			  int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
				LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			    BrandBean band=SellerProductCallService.getBrandInfo(13217);
			    liveProductsWapper.updatePBrandByLiveAndBrank(lid,band.getBrandId(),band.getBrandName());
				bean.setLiveId(lid);
				bean.setSearchType(searchTypeEnum.BrandName);
				bean.setKeyword("Baby Banz");
				call.setData(bean);
				call.callService();	
				GetVerify(bean);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_004() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌名称,并且查询关键字不为空并且关键字都为大写,返回查询关键字的有效的直播商品id列表");
		try {
			  int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
				LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			    BrandBean band=SellerProductCallService.getBrandInfo(13217);
			    liveProductsWapper.updatePBrandByLiveAndBrank(lid,band.getBrandId(),band.getBrandName());
				bean.setLiveId(lid);
				bean.setSearchType(searchTypeEnum.BrandName);
				bean.setKeyword("BABY BANZ");
				call.setData(bean);
				call.callService();	
				GetVerify(bean);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_005() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌名称,并且查询关键字不为空并且关键字都为小写,返回查询关键字的有效的直播商品id列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
		    BrandBean band=SellerProductCallService.getBrandInfo(13217);
		    liveProductsWapper.updatePBrandByLiveAndBrank(lid,band.getBrandId(),band.getBrandName());
			bean.setLiveId(lid);
			bean.setSearchType(searchTypeEnum.BrandName);
			bean.setKeyword("baby banz");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_006() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按类别名称,并且品牌编号列表为空,返回全部有效的直播商品id列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			//bean.setLiveId(176706);
			bean.setSearchType(searchTypeEnum.CategoryName);
			bean.setKeyword("");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_007() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按类别名称,并且品牌编号列表不为空,返回品牌编号列表的直播商品id列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.CategoryName);
			bean.setKeyword("按键手机");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_008() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌和分类编号查询,并且品牌编号列表和类别编号列表不为空,返回根据品牌编号列表和类别编号列表查询有效的直播商品id列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10145");
			bean.setThirdCategoryIdList("1003");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_009() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌和分类编号查询,并且品牌编号列表和类别编号列表不为空和品牌编号列表和类别编号列表为分别多个有效的数据,返回根据品牌编号列表和类别编号列表查询有效的直播商品id列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10145");
			bean.setThirdCategoryIdList("1002,1003");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_010() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌和分类编号查询,并且品牌编号列表和类别编号列表不为空和品牌编号列表和类别编号列表为分别多个有效无效混合的数据,返回根据品牌编号列表和类别编号列表查询有效的直播商品id列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10145,10146");
			bean.setThirdCategoryIdList("1002,1003,1001");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_011() {
		Logger.start(false,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌和分类编号查询,并且品牌编号列表和类别编号列表不为空和品牌编号列表和类别编号列表为分别无效的数据,返回根据品牌编号列表和类别编号列表查询列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10146");
			bean.setThirdCategoryIdList("1001");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_012() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌和分类编号查询,并且品牌编号列表为空,类别编号列表不为空,返回空列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("");
			bean.setThirdCategoryIdList("1002,1003");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_013() {
		Logger.start(true,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌和分类编号查询,并且品牌编号列表不为空,类别编号列表为空,返回根据品牌编号列表查询有效的直播商品id列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10145");
			bean.setThirdCategoryIdList("");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_014() {
		Logger.start(false,"根据直播Id获取商品Id列表-直播Id不为空,搜索类型:按品牌和分类编号查询,并且品牌编号列表不为空,类别编号列表为空和类别编号列表为无效的数据,返回空列表");
		try {
			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
			Thread.sleep(1000); 
			bean.setLiveId(lid);
			//bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10145");
			bean.setThirdCategoryIdList("");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetProductListByLiveId_015() {
		Logger.start(false,"根据直播Id获取商品Id列表-直播Id为空,返回异常,提示直播id不能为空");
		try {
			bean.setLiveId(99999);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10145");
			bean.setThirdCategoryIdList("1003");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductListByLiveId_016() {
		Logger.start(false,"查询过期的商品直播,返回空列表");
		try {
//			int lid=LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
//			LiveCallService.CreateLivesProductByLivesId(sellerId,sellerName,lid);
//			Thread.sleep(1000); 
			//bean.setLiveId(lid);
			bean.setLiveId(176714);
			bean.setSearchType(searchTypeEnum.BrandIdAndCategoryId);
			bean.setBrandIdList("10145");
			bean.setThirdCategoryIdList("");
			call.setData(bean);
			call.callService();	
			GetVerify(bean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void GetVerify(GetProductListByLiveIdBean bean) throws Exception
	{
		LiveProductsWapper liveProductsWapper=new LiveProductsWapper();
		//ArrayList<Map>  lplst=liveProductsWapper.getLiveProductsBylidAndNum(bean.getLiveId(), 0, "{'sort':1,'add':-1}");
		ArrayList<Map>  lplst=liveProductsWapper.getLiveProductsBylid(bean.getLiveId(), 0, "{'sort':1,'add':-1}");
		if(lplst.size()>0)
		{
			List<Object> proIds=new ArrayList<Object>();
			switch(bean.getSearchType().getIndex())
			{
			case 0:
				proIds= lplst.stream().map(x->x.get("spid")).collect(Collectors.toList());
				break;
			case 1:
				if(bean.getKeyword()!="")
				   proIds=lplst.stream().filter(x->String.valueOf(x.get("brand")).equalsIgnoreCase(bean.getKeyword()))
				.map(x->x.get("spid")).collect(Collectors.toList());
				else
					proIds= lplst.stream().map(x->x.get("spid")).collect(Collectors.toList());
				break;
			case 2:
				if(bean.getKeyword()!="")
			     proIds=lplst.stream().filter(x->String.valueOf(x.get("tcatname")).equalsIgnoreCase(bean.getKeyword()))
				.map(x->x.get("spid")).collect(Collectors.toList());
				else
					proIds= lplst.stream().map(x->x.get("spid")).collect(Collectors.toList());
				break;
			case 3:
				String[] brandIds=IsNull(bean.getBrandIdList())?new String[0]:bean.getBrandIdList().split(",");
				String[] categoryIds=IsNull(bean.getThirdCategoryIdList())?new String[0]:bean.getThirdCategoryIdList().split(",");
				
				if(brandIds.length>0 && categoryIds.length>0)
				{
					proIds=lplst.stream().filter(x->{return Arrays.stream(brandIds).anyMatch(s->s.equals(String.valueOf(x.get("bid"))))
							&& Arrays.stream(categoryIds).anyMatch(s->s.equals(String.valueOf(x.get("tcatid")))
									|| s.equals(String.valueOf(x.get("scatid")))
							);
							}).map(x->x.get("spid")).collect(Collectors.toList());
				}
				else if(brandIds.length>0)
				{
					proIds=lplst.stream().filter(x->{return Arrays.stream(brandIds).anyMatch(s->s.equals(String.valueOf(x.get("bid"))));
							}).map(x->x.get("spid")).collect(Collectors.toList());
				}
				else if(categoryIds.length>0)
				{
					proIds=lplst.stream().filter(x->{return Arrays.stream(categoryIds).anyMatch(s->s.equals(String.valueOf(x.get("tcatid")))
							||s.equals(String.valueOf(x.get("scatid"))) 
							);
							}).map(x->x.get("spid")).collect(Collectors.toList());
				}
				break;
			}
			VerifyMessage(proIds);
		}
	}
	private void VerifyMessage(List<Object> proIds) throws JSONException
	{
		Logger.verifyEquals(200, call.getCode(), "判断code是否为200");
		Logger.verifyEquals("操作成功",call.getMsg(), "判断Msg返回操作成功");
		Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
		Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
		Logger.verifyEquals(proIds.size(), call.getProductList().length(), "验证直播商品id是否与mongodb一致");
		for(int i=0;i<proIds.size();i++)
		{
			
			Logger.verifyEquals(proIds.get(i).toString(), call.getProductList().get(i).toString(), "验证第"+i+"个直播商品id是否与mongodb一致");
		}
	}
	 private boolean IsNull(String bcIds)
	 {
		 if(bcIds!="" || bcIds.contains(","))
			 return  false;
		 else
			 return true;
	 }
}
	

